package com.hawthorn.redis.service.company.impl;

import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.entity.dto.SysCompanyDTO;
import com.hawthorn.entity.po.SysCompanyPO;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.redis.repository.CompanyCacheMapper;
import com.hawthorn.redis.service.company.CompanyCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>todo 加载公司缓存服务实现类 </p>
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 2022/06/30 12:32 PM
 * @version v1.0.2
 * @copyright: Copyright (c)  andyten
 */
@Service
@Slf4j
public class CompanyCacheServiceImpl implements CompanyCacheService
{
  @Resource
  RedisMyClient redisMyClient;

  @Resource
  private CompanyCacheMapper companyCacheMapper;

  @Transactional(readOnly = true)
  public void init()
  {
    List<SysCompanyDTO> companyCache = companyCacheMapper.selectCompany();
    String key;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_COMPANY_PREFIX, "*"));

    for (SysCompanyDTO sysCompanyDTO : companyCache)
    {
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_COMPANY_PREFIX, sysCompanyDTO.getId());

      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_CODE, sysCompanyDTO.getCode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_CNAME, sysCompanyDTO.getCname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_SNAME, sysCompanyDTO.getSname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_ADDRESS, MyUtil.getValue(sysCompanyDTO.getAddress()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_CONTACT, MyUtil.getValue(sysCompanyDTO.getContact()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_LEGALPERSON, MyUtil.getValue(sysCompanyDTO.getLegalPerson()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_INVOICENAME, MyUtil.getValue(sysCompanyDTO.getInvoiceName()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_TYPE, MyUtil.getValue(sysCompanyDTO.getType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_KIND, MyUtil.getValue(sysCompanyDTO.getKind()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_BIZTYPE, MyUtil.getValue(sysCompanyDTO.getBizType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_STATUS, MyUtil.getValue(sysCompanyDTO.getStatus()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_BIZTYPE, MyUtil.getValue(sysCompanyDTO.getOemCompanyId()));
    }

    log.info("====== load companydata to redis success ======");
  }

  @Override
  public void cacheCompanyById(List<SysCompanyPO> companyPOList)
  {
    log.info("====== 执行开始======");
    String key;
    for (SysCompanyPO sysCompanyPO : companyPOList)
    {
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_COMPANY_PREFIX, sysCompanyPO.getId());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_CODE, sysCompanyPO.getCode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_CNAME, sysCompanyPO.getCname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_SNAME, sysCompanyPO.getSname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_ADDRESS, MyUtil.getValue(sysCompanyPO.getAddress()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_CONTACT, MyUtil.getValue(sysCompanyPO.getContact()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_LEGALPERSON, MyUtil.getValue(sysCompanyPO.getLegalPerson()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_INVOICENAME, MyUtil.getValue(sysCompanyPO.getInvoiceName()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_TYPE, MyUtil.getValue(sysCompanyPO.getType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_KIND, MyUtil.getValue(sysCompanyPO.getKind()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_BIZTYPE, MyUtil.getValue(sysCompanyPO.getBizType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_COMPANY_STATUS, MyUtil.getValue(sysCompanyPO.getStatus()));
    }

    log.info("====== 执行结束 ======");
  }
}
